package com.nbBoot.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nbBoot.common.enums.ResMsg;

import java.util.Map;

import static com.nbBoot.common.enums.ResMsg.ERROR;
import static com.nbBoot.common.enums.ResMsg.SUCCESS;

/**
 * 统一返回对象
 *
 * @author 王永吉
 * @date 2019/8/21
 */
public class SRS extends JSONObject {
    public static SRS create() {
        return new SRS();
    }

    public static SRS bySuccess() {
        return create()
                .code(SUCCESS.code_())
                .message(SUCCESS.message_())
                .success(SUCCESS.isSuccess_())
                .data(new JSONObject());
    }

    public static SRS byError() {
        return create()
                .code(ERROR.code_())
                .message(ERROR.message_())
                .success(ERROR.isSuccess_())
                .data(new JSONObject());
    }

    public static SRS byResponse(ResMsg response) {
        return create()
                .code(response.code_())
                .message(response.message_())
                .success(response.isSuccess_())
                .data(new JSONObject());
    }

    public static SRS byResponse(ResMsg response, Object data) {
        return byResponse(response).data(data);
    }

    public static JSONObject parseObj(Object obj) {
        return JSON.parseObject(JSON.toJSONString(obj));
    }

    public SRS data(Object data) {
        if (data instanceof Map) {
            System.out.println("map");
        }
        if (data instanceof String) {
            System.out.println("map");
        }
        if (data instanceof Number) {
            System.out.println("Number");
        }
        return this.set("data", data);
    }

    public SRS message(Object message) {
        return this.set("message", message);
    }

    public SRS code(Object code) {
        return this.set("code", code);
    }

    public SRS success(Object success) {
        return this.set("success", success);
    }

    public static SRS create(String key, Object value) {
        return create().set(key, value);
    }

    public static SRS byError(Integer code) {
        return byError().code(code);
    }

    public static SRS byError(String message) {
        return byError().message(message);
    }

    public static SRS byError(Object data) {
        return byError().data(data);
    }

    public static SRS byError(ResMsg response) {
        return byResponse(response);
    }

    public static SRS byError(Integer code, String message) {
        return byError().code(code).message(message);
    }

    public static SRS byError(Integer code, Object data) {
        return byError().code(code).data(data);
    }

    public static SRS byError(String key, Object value) {
        return byError().set(key, value);
    }

    public static SRS byError(Integer code, String message, Object data) {
        return byError().code(code).data(data).message(message);
    }

    public static SRS bySuccess(String key, Object value) {
        return bySuccess().set(key, value);
    }

    public static SRS bySuccess(Integer code) {
        return bySuccess().code(code);
    }

    public static SRS bySuccess(Integer code, String message) {
        return bySuccess().code(code).message(message);
    }

    public static SRS bySuccess(Integer code, String message, Object data) {
        return bySuccess().code(code).data(data).message(message);
    }

    public static SRS bySuccess(Integer code, Object data) {
        return bySuccess().code(code).data(data);
    }

    public static SRS bySuccess(String message) {
        return bySuccess().message(message);
    }

    public static SRS bySuccess(Object data) {
        return bySuccess().data(data);
    }

    public static SRS parseSRS(Object data) {
        if (data instanceof String) {
            return JSON.parseObject(data.toString(), SRS.class);
        }
        return JSON.parseObject(JSON.toJSONString(data), SRS.class);
    }

    /**
     * 复制对象并重新开辟内存空间
     * <p>
     * 前提是此对象可以转换为 JsonString
     */
    public static JSONObject copyObj(Object object) {
        return JSON.parseObject(JSON.toJSONString(object));
    }

    /**
     * 复制对象并重新开辟内存空间
     * <p>
     * 前提是此对象可以转换为 JsonString
     */
    public static JSONArray copyArray(Object arrayObject) {
        return JSON.parseArray(JSON.toJSONString(arrayObject));
    }

    //
    //
    //
    //
    //
    public SRS set(String key, Object value) {
        super.put(key, value);
        return this;
    }

    /**
     * 复制对象并重新开辟内存空间
     * 前提是此对象可以转换为 JsonString
     */
    public SRS copy() {
        return JSON.parseObject(this.toJSONString(), SRS.class);
    }


    /**
     * 返回对象的数组字符串
     */
    public static Object parseArrayStr(Object arrayObject) {
        return JSON.parseArray(JSON.toJSONString(arrayObject)).toJSONString();
    }

    public String str() {
        return this.toJSONString();
    }
}
